<template>
  <div class=''>

    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card class="box-card">

      <!-- 添加分类 -->
      <el-row :gutter="20">
        <el-col :span='4'>
          <el-button type="primary" @click="showCateDialogVisible">添加分类</el-button>
        </el-col>
      </el-row>

      <!-- 表格 -->
      <tree-table
      :data="cateList" :columns="columns" show-index
      :selection-type="false" :expand-type="false"
      border
      >

        <!-- 是否激活 -->
        <template slot="isok" slot-scope="scope">
          <i :class="[
            scope.row.cat_deleted === false ? 'el-icon-success success' : 'el-icon-error error'
          ]"></i>
        </template>

        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
            <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
            <el-tag type="success" v-else-if="scope.row.cat_level === 1" >二级</el-tag>
            <el-tag type="warning" v-else>三级</el-tag>
        </template>

        <!-- 操作 -->
        <template slot="opt" slot-scope="scope">

            <!-- 修改分类按钮 -->
            <el-button
              type="primary" icon="el-icon-edit" size="mini"
              @click="showEditRoleDialog(scope.row)"
            >编辑</el-button>

            <!-- 删除分类按钮 -->
            <el-button
              type="danger" icon="el-icon-delete" size="mini"
              @click="removeCateById(scope.row.cat_id)"
            >删除</el-button>

        </template>

      </tree-table>

      <!-- 分页区域 -->
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="queryinfo.pagenum"
        :page-size="queryinfo.pagesize"
        layout="total, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>

    <!-- 添加分类的对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      width="50%"
      @close="addCateDialogClosed"
      >

      <!-- 内容主体区域 -->
      <el-form
      :model="addCateFrom"
      :rules="addCateFromRules"
      label-width="100px"
      ref="addCateFromRef"
      >
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="addCateFrom.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父类分类:">
            <el-cascader
              v-model="selectedKeys"
              :options="parentCateList"
              :props="cascaderProps"
              @change="parentCateChange"
            ></el-cascader>
        </el-form-item>
      </el-form>

      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑分类的对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="editCateDialogVisible"
      width="50%"
      @close="editCateDialogClosed"
      >

      <!-- 内容主体区域 -->
      <el-form
      :model="editCateFrom"
      :rules="editCateFromRules"
      label-width="100px"
      ref="editCateFromRef"
      >
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="editCateFrom.cat_name"></el-input>
        </el-form-item>
      </el-form>

      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: 'Cate',
  props: {

  },
  data () {
    return {
      cateList: [],
      total: 0,
      queryinfo: {
        type: 3,
        // 当前的 页数
        pagenum: 1,
        // 单前每页显示多少条数据
        pagesize: 5
      },
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isok'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ],
      // 控制添加分类对话框的显示和隐藏
      addCateDialogVisible: false,
      parentCateList: [],
      selectedKeys: [],
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        checkStrictly: true,
        expandTrigger: 'hover'
      },
      addCateFrom: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      addCateFromRules: {
        cat_name: [
          { required: true, message: '请输入类名', trigger: 'blur' }
        ]
      },
      // 编辑分类相关数据
      editCateDialogVisible: false,
      editCateFrom: {},
      editCateFromRules: {
        cat_name: [
          { required: true, message: '请输入类名', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    // 获取商品分类列表
    async getCateList () {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryinfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败')
      }
      if (res.data.result.length === 0 && res.data.total !== 0) {
        this.queryinfo.pagenum -= 1
        this.getUserList()
        return
      }
      this.cateList = res.data.result
      this.total = res.data.total
    },

    // 获取商品分类列表
    async getParentCateList () {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败')
      }
      this.parentCateList = res.data
    },

    // 监听 页码值 改变的事件
    handleCurrentChange (NewPage) {
      this.queryinfo.pagenum = NewPage
      this.getCateList()
    },

    // 显示 添加分类对话框
    showCateDialogVisible () {
      this.getParentCateList()
      this.addCateDialogVisible = true
    },

    // 监听 添加分类对话框的关闭
    addCateDialogClosed () {
      this.$refs.addCateFromRef.resetFields()
      this.selectedKeys = []
      this.addCateFrom.cat_pid = 0
      this.addCateFrom.cat_level = 0
    },

    // 监听联机选择框
    parentCateChange () {
      // 判断是否有选中父级
      if (this.selectedKeys.length > 0) {
        this.addCateFrom.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
        this.addCateFrom.cat_level = this.selectedKeys.length
      } else {
        this.addCateFrom.cat_pid = 0
        this.addCateFrom.cat_level = 0
      }
    },

    // 添加分类
    addCate () {
      this.$refs.addCateFromRef.validate(async valid => {
        if (!valid) return

        // 发起添加用户请求
        const { data: res } = await this.$http.post('categories', this.addCateFrom)
        if (res.meta.status !== 201) return this.$message.error('分类添加失败')
        this.$message.success('分类添加成功')

        // 隐藏添加用户对话框
        this.addCateDialogVisible = false

        // 重新获取用户列表
        this.getCateList()
      })
    },

    // 展示编辑分类对话框
    async showEditRoleDialog (role) {
      // 根据用户id,查询用户信息
      const { data: res } = await this.$http.get('categories/' + role.cat_id)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.editCateFrom = res.data

      this.editCateDialogVisible = true
    },

    // 监听 编辑对话框的关闭
    editCateDialogClosed () {
      this.$refs.editCateFromRef.resetFields()
    },

    // 点击确认，编辑分类
    editCate () {
      this.$refs.editCateFromRef.validate(async valid => {
        if (!valid) return

        // 发起编辑角色请求
        const { data: res } = await this.$http.put('categories//' + this.editCateFrom.cat_id, {
          cat_name: this.editCateFrom.cat_name
        })
        if (res.meta.status !== 200) return this.$message.error('分类编辑失败')
        this.$message.success('分类编辑成功')

        // 重新获取用户列表
        this.getCateList()

        // 隐藏编辑角色对话框
        this.editCateDialogVisible = false
      })
    },

    // 删除分类
    async removeCateById (id) {
      // 弹框提示用户是否删除
      const confirmResult = await this.$confirm('此操作将永久删除该分类', '提示', {
        cancelButtonText: '取消',
        confirmButtonText: '确定',
        type: 'warning'
      }).catch(err => {
        return err
      })

      // 判断是否发起删除角色请求
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }

      // 发起删除角色请求
      const { data: res } = await this.$http.delete('categories/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('删除分类失败')
      }
      this.$message.success('角色分类成功')

      // 重新获取用户列表
      this.getCateList()
    }
  }
}
</script>

<style  scoped>
.el-breadcrumb{
  margin-bottom: 15px;
  font-size: 12px;
}
.box-card{
  box-shadow:  0 1px 1px rgba(0, 0, 0, 0.15) !important;
}
.zk-table{
  margin-bottom: 15px;
  margin-top: 15px;
}
.success{
  color: #67C23A;
}
.error{
  color: #F56C6C;
}
.el-cascader{
  width: 100% !important;
}
</style>
